﻿Namespace DotNetNuke.Modules.ActiveForums
    Public Class DailyDigest
#If SKU = "ENTERPRISE" Then
        Inherits DotNetNuke.Services.Scheduling.SchedulerClient
        Public Sub New(ByVal objScheduleHistoryItem As DotNetNuke.Services.Scheduling.ScheduleHistoryItem)
            MyBase.New()
            Me.ScheduleHistoryItem = objScheduleHistoryItem
        End Sub
        Public Overrides Sub DoWork()
            Try


                Subscriptions.SendSubscriptions(SubscriptionTypes.DailyDigest, Now())
                ScheduleHistoryItem.Succeeded = True
                ScheduleHistoryItem.TimeLapse = GetElapsedTimeTillNextStart()
                ScheduleHistoryItem.AddLogNote("Daily Digest Complete")

            Catch ex As Exception
                ScheduleHistoryItem.Succeeded = False
                ScheduleHistoryItem.AddLogNote("Daily Digest Failed: " + ex.ToString)
                Errored(ex)
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
        End Sub

        Private Shared Function GetElapsedTimeTillNextStart() As Integer
            Dim NextRun As DateTime = Now.AddDays(1)
            Dim nextStart As New DateTime(NextRun.Year, NextRun.Month, NextRun.Day, 18, 0, 0)
            Dim elapseMinutes As Integer = CInt((nextStart.Ticks - DateTime.Now.Ticks) \ TimeSpan.TicksPerDay)
            Return elapseMinutes
        End Function

#End If


    End Class
End Namespace